/*
 * Description: NetCard config in windows
 */
 
#ifndef NETCARD_H
#define NETCARD_H

#include <string>
#include <memory>
#include <vector>
#include <Wbemidl.h>
#include <atlbase.h>

class NetCard {
public:
    NetCard() {}
    explicit NetCard(const std::string &key)
        :netKey(key) {}
    ~NetCard()
    {
        Clear();
    }
    // 设置网络设备GUID
    void SetKey(const std::string &key)
    {
        Clear();
        netKey = key;
    }
    // 启用DHCP
    bool EnableDhcp();
    // 启动静态IP,设置IP、掩码
    bool SetIpConfig(const std::string &ip, const std::string &mask);
    // 设置网关
    bool SetGateway(const std::string &gateway);
    // 设置DNS地址
    bool SetDns(const std::string &defaultDns, const std::string &backupDns);
    // 设置自动DNS
    bool SetAutoDns();

private:
    NetCard(const NetCard &rhs) = delete;
    NetCard &operator = (const NetCard &rhs) = delete;
    bool Init();
    void Clear();
    bool getNetAdapter(HRESULT &hres);
    std::shared_ptr<SAFEARRAY> CreateSafeArray(const std::vector<std::string> &args);
    bool SetDnsBase(bool isAuto, const std::string &defaultDns, const std::string &backupDns);
    bool ExecuteMethod(const wchar_t *method, CComPtr<IWbemClassObject> paramsInstance);

private:
    std::string netKey;
    bool isInit = false;
    CComPtr<IWbemLocator> pInst = nullptr;
    CComPtr<IWbemServices> pService = nullptr;
    CComPtr<IEnumWbemClassObject> pEnum = nullptr;
    CComPtr<IWbemClassObject> pObj = nullptr;
    CComPtr<IWbemClassObject> pConfig = nullptr;
    VARIANT netPath;
};
#endif